/**
 * created by 小卷毛, 2022-02-28
 * Copyright (c) 2022, 416143467@qq.com All Rights Reserved.
 * #                   *********                            #
 * #                  ************                          #
 * #                  *************                         #
 * #                 **  ***********                        #
 * #                ***  ****** *****                       #
 * #                *** *******   ****                      #
 * #               ***  ********** ****                     #
 * #              ****  *********** ****                    #
 * #            *****   ***********  *****                  #
 * #           ******   *** ********   *****                #
 * #           *****   ***   ********   ******              #
 * #          ******   ***  ***********   ******            #
 * #         ******   **** **************  ******           #
 * #        *******  ********************* *******          #
 * #        *******  ******************************         #
 * #       *******  ****** ***************** *******        #
 * #       *******  ****** ****** *********   ******        #
 * #       *******    **  ******   ******     ******        #
 * #       *******        ******    *****     *****         #
 * #        ******        *****     *****     ****          #
 * #         *****        ****      *****     ***           #
 * #          *****       ***        ***      *             #
 * #            **       ****        ****                   #
 */
package com.rzico.sbl.viewmodel

import com.lzg.extend.BaseResponse
import com.lzg.extend.jackson.JacksonConvert
import com.lzg.okrx.RxSingleHelper
import com.lzg.okrx.adapter.FlowableBody
import com.lzy.okgo.OkGo
import com.rzico.sbl.R
import com.rzico.sbl.model.*
import com.rzico.sbl.ui.report.other.ReportUrl
import com.xinnuo.common_ui.utils.applyFlowableSchedulers
import com.xinnuo.common_ui.utils.applySchedulers
import com.xinnuo.common_ui.utils.subscribeByFinally
import io.reactivex.Single

/**
 * 描 述：库管首页操作相关
 * 作 者：小卷毛  2022-02-28 09:19
 * 修改描述：
 * 修 改 人：
 * 修改版本：
 */
class ReportDepotViewModel : ColorViewModel() {

    // 总库管
    fun chiefIcons() = arrayListOf(
        ManagerContent("库管", R.mipmap.icon_depot01, "库管"),
        ManagerContent("出库", R.mipmap.icon_depot03, "出库"),
        ManagerContent("入库", R.mipmap.icon_depot04, "入库"),
        ManagerContent("入空桶", R.mipmap.icon_depot05, "入空桶")
    )

    // 副库管
    fun deputyIcons() = arrayListOf(
        ManagerContent("出库", R.mipmap.icon_depot03, "出库"),
        ManagerContent("入库", R.mipmap.icon_depot04, "入库"),
        ManagerContent("入空桶", R.mipmap.icon_depot05, "入空桶")
    )

    /**
     * 出库列表
     */
    fun outList(
        page: Int,
        status: String,
        onSuccess: (ArrayList<DepotOutData>) -> Unit = {},
        onFinally: () -> Unit = {}
    ) = mDisposables.add(
        OkGo.get<BaseResponse<ArrayList<DepotOutData>>>(ReportUrl.getBaseUrl().depotOut)
            .params(
                getParams(
                    "status" to status,
                    "pageStart" to page * 20,
                    "pageSize" to 20
                )
            )
            .converter(object : JacksonConvert<BaseResponse<ArrayList<DepotOutData>>>() {})
            .adapt(FlowableBody())
            .compose(applyFlowableSchedulers())
            .map { it.data }
            .subscribeByFinally(
                event = this,
                onSuccess = onSuccess,
                onFinally = onFinally
            )
    )

    /**
     * 司机列表
     */
    fun driverList(
        page: Int,
        keyWord: String,
        onSuccess: (ArrayList<ManagerStation>) -> Unit = {},
        onFinally: () -> Unit = {}
    ) = mDisposables.add(
        OkGo.get<BaseResponse<ArrayList<ManagerStation>>>(ReportUrl.getBaseUrl().depotDriver)
            .params(
                getParams(
                    "searchValue" to keyWord,
                    "pageStart" to page * 20,
                    "pageSize" to 20
                )
            )
            .converter(object : JacksonConvert<BaseResponse<ArrayList<ManagerStation>>>() {})
            .adapt(FlowableBody())
            .compose(applyFlowableSchedulers())
            .map { it.data }
            .subscribeByFinally(
                event = this,
                onSuccess = onSuccess,
                onFinally = onFinally
            )
    )

    /**
     * 司机订单列表
     */
    fun orderList(
        page: Int,
        memberId: String,
        isLoading: Boolean = true,
        onSuccess: (ArrayList<OrderData>) -> Unit = {},
        onFinally: () -> Unit = {}
    ) = mDisposables.add(
        OkGo.get<BaseResponse<OrderModel>>(ReportUrl.getBaseUrl().depotOrder)
            .params(
                getParams(
                    "memberId" to memberId,
                    "pageStart" to page * 20,
                    "pageSize" to 20
                )
            )
            .converter(object : JacksonConvert<BaseResponse<OrderModel>>() {})
            .adapt(FlowableBody())
            .compose(applyFlowableSchedulers())
            .doOnSubscribe { if (isLoading) showLoading() }
            .doFinally { if (isLoading) dismissLoading() }
            .map { it.data.data }
            .subscribeByFinally(
                event = this,
                onSuccess = onSuccess,
                onFinally = onFinally
            )
    )

    /**
     * 司机订单数量
     */
    fun orderCount(): Single<String> =
        RxSingleHelper
            .requestString(
                url = ReportUrl.getBaseUrl().depotOrdCut
            )
            .compose(applySchedulers())

    /**
     * 入库列表
     */
    fun inList(
        page: Int,
        status: String,
        onSuccess: (ArrayList<DepotOutData>) -> Unit = {},
        onFinally: () -> Unit = {}
    ) = mDisposables.add(
        OkGo.get<BaseResponse<ArrayList<DepotOutData>>>(ReportUrl.getBaseUrl().depotIn)
            .params(
                getParams(
                    "status" to status,
                    "pageStart" to page * 20,
                    "pageSize" to 20
                )
            )
            .converter(object : JacksonConvert<BaseResponse<ArrayList<DepotOutData>>>() {})
            .adapt(FlowableBody())
            .compose(applyFlowableSchedulers())
            .map { it.data }
            .subscribeByFinally(
                event = this,
                onSuccess = onSuccess,
                onFinally = onFinally
            )
    )

    /**
     * 库管列表
     */
    fun depotList(
        page: Int,
        onSuccess: (ArrayList<DepotManager>) -> Unit = {},
        onFinally: () -> Unit = {}
    ) = mDisposables.add(
        OkGo.get<BaseResponse<ArrayList<DepotManager>>>(ReportUrl.getBaseUrl().depotList)
            .params(
                getParams(
                    "pageStart" to page * 20,
                    "pageSize" to 20
                )
            )
            .converter(object : JacksonConvert<BaseResponse<ArrayList<DepotManager>>>() {})
            .adapt(FlowableBody())
            .compose(applyFlowableSchedulers())
            .map { it.data }
            .subscribeByFinally(
                event = this,
                onSuccess = onSuccess,
                onFinally = onFinally
            )
    )

    /**
     * 桶数/订单柱状图数据
     */
    fun barrelTotal(
        beginDate: String,
        endDate: String,
        adminId: String = "",
        onSuccess: (ArrayList<DepotBarrelDate>) -> Unit = {},
        onFinally: () -> Unit = {}
    ) = mDisposables.add(
        OkGo.get<BaseResponse<ArrayList<DepotBarrelDate>>>(ReportUrl.getBaseUrl().depotSumBO)
            .params(
                getParams(
                    "adminId" to adminId,
                    "beginDate" to beginDate,
                    "endDate" to endDate
                )
            )
            .converter(object : JacksonConvert<BaseResponse<ArrayList<DepotBarrelDate>>>() {})
            .adapt(FlowableBody())
            .compose(applyFlowableSchedulers())
            .map { it.data }
            .subscribeByFinally(
                event = this,
                onSuccess = onSuccess,
                onFinally = onFinally
            )
    )

    /**
     * 所有出库商品柱状图
     */
    fun goodsList(
        adminId: String,
        beginDate: String,
        endDate: String,
        onSuccess: (ArrayList<ManagerGoodsData>) -> Unit = {},
        onFinally: () -> Unit = {}
    ) = mDisposables.add(
        OkGo.get<BaseResponse<ArrayList<ManagerGoodsData>>>(ReportUrl.getBaseUrl().depotOutSum)
            .params(
                getParams(
                    "adminId" to adminId,
                    "beginDate" to beginDate,
                    "endDate" to endDate
                )
            )
            .converter(object : JacksonConvert<BaseResponse<ArrayList<ManagerGoodsData>>>() {})
            .adapt(FlowableBody())
            .compose(applyFlowableSchedulers())
            .map { it.data }
            .subscribeByFinally(
                event = this,
                onSuccess = onSuccess,
                onFinally = onFinally
            )
    )

    /**
     * 单个出库商品柱状图
     */
    fun goodsRangeList(
        goodsSn: String,
        adminId: String,
        beginDate: String,
        endDate: String,
        onSuccess: (ArrayList<ManagerDate>) -> Unit = {},
        onFinally: () -> Unit = {}
    ) = mDisposables.add(
        OkGo.get<BaseResponse<ArrayList<ManagerDate>>>(ReportUrl.getBaseUrl().depotOutDet)
            .params(
                getParams(
                    "sn" to goodsSn,
                    "adminId" to adminId,
                    "beginDate" to beginDate,
                    "endDate" to endDate
                )
            )
            .converter(object : JacksonConvert<BaseResponse<ArrayList<ManagerDate>>>() {})
            .adapt(FlowableBody())
            .compose(applyFlowableSchedulers())
            .map { it.data }
            .subscribeByFinally(
                event = this,
                onSuccess = onSuccess,
                onFinally = onFinally
            )
    )

    /**
     * 所有品牌入库柱状图
     */
    fun brandList(
        adminId: String,
        beginDate: String,
        endDate: String,
        onSuccess: (ArrayList<ManagerGoodsData>) -> Unit = {},
        onFinally: () -> Unit = {}
    ) = mDisposables.add(
        OkGo.get<BaseResponse<ArrayList<ManagerGoodsData>>>(ReportUrl.getBaseUrl().depotInSum)
            .params(
                getParams(
                    "adminId" to adminId,
                    "beginDate" to beginDate,
                    "endDate" to endDate
                )
            )
            .converter(object : JacksonConvert<BaseResponse<ArrayList<ManagerGoodsData>>>() {})
            .adapt(FlowableBody())
            .compose(applyFlowableSchedulers())
            .map { it.data }
            .subscribeByFinally(
                event = this,
                onSuccess = onSuccess,
                onFinally = onFinally
            )
    )

    /**
     * 单个品牌入库柱状图
     */
    fun brandRangeList(
        brand: String,
        adminId: String,
        beginDate: String,
        endDate: String,
        onSuccess: (ArrayList<ManagerDate>) -> Unit = {},
        onFinally: () -> Unit = {}
    ) = mDisposables.add(
        OkGo.get<BaseResponse<ArrayList<ManagerDate>>>(ReportUrl.getBaseUrl().depotIn2)
            .params(
                getParams(
                    "name" to brand,
                    "adminId" to adminId,
                    "beginDate" to beginDate,
                    "endDate" to endDate
                )
            )
            .converter(object : JacksonConvert<BaseResponse<ArrayList<ManagerDate>>>() {})
            .adapt(FlowableBody())
            .compose(applyFlowableSchedulers())
            .map { it.data }
            .subscribeByFinally(
                event = this,
                onSuccess = onSuccess,
                onFinally = onFinally
            )
    )

    /**
     * 外品牌入库柱状图
     */
    fun otherList(
        adminId: String,
        beginDate: String,
        endDate: String,
        onSuccess: (ArrayList<ManagerDate>) -> Unit = {},
        onFinally: () -> Unit = {}
    ) = mDisposables.add(
        OkGo.get<BaseResponse<ArrayList<ManagerDate>>>(ReportUrl.getBaseUrl().depotOther)
            .params(
                getParams(
                    "adminId" to adminId,
                    "beginDate" to beginDate,
                    "endDate" to endDate
                )
            )
            .converter(object : JacksonConvert<BaseResponse<ArrayList<ManagerDate>>>() {})
            .adapt(FlowableBody())
            .compose(applyFlowableSchedulers())
            .map { it.data }
            .subscribeByFinally(
                event = this,
                onSuccess = onSuccess,
                onFinally = onFinally
            )
    )

    /**
     * 异常订单曲线图
     */
    fun errorList(
        beginDate: String,
        endDate: String,
        adminId: String = "",
        onSuccess: (ArrayList<ManagerDate>) -> Unit = {},
        onFinally: () -> Unit = {}
    ) = mDisposables.add(
        OkGo.get<BaseResponse<ArrayList<ManagerDate>>>(ReportUrl.getBaseUrl().depotError)
            .params(
                getParams(
                    "adminId" to adminId,
                    "beginDate" to beginDate,
                    "endDate" to endDate
                )
            )
            .converter(object : JacksonConvert<BaseResponse<ArrayList<ManagerDate>>>() {})
            .adapt(FlowableBody())
            .compose(applyFlowableSchedulers())
            .map { it.data }
            .subscribeByFinally(
                event = this,
                onSuccess = onSuccess,
                onFinally = onFinally
            )
    )

    /**
     * 合并单数量
     */
    fun depotCount(): Single<String> =
        RxSingleHelper
            .requestString(
                url = ReportUrl.getBaseUrl().depotCut
            )
            .compose(applySchedulers())

}